"use strict";
cc._RF.push(module, 'f3903fJFh9D85nIWFGQJpbF', 'RotateAround');
// eazax-ccc/components/RotateAround.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Axis = void 0;
/** 轴 */
var Axis;
(function (Axis) {
    /** 正 X 轴 */
    Axis[Axis["PositiveX"] = 0] = "PositiveX";
    /** 正 Y 轴 */
    Axis[Axis["PositiveY"] = 1] = "PositiveY";
    /** 负 X 轴 */
    Axis[Axis["NegativeX"] = 2] = "NegativeX";
    /** 负 Y 轴 */
    Axis[Axis["NegativeY"] = 3] = "NegativeY";
})(Axis = exports.Axis || (exports.Axis = {}));
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
/**
 * 围绕旋转组件
 * @see RotateAround.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/components/RotateAround.ts
 */
var RotateAround = /** @class */ (function (_super) {
    __extends(RotateAround, _super);
    function RotateAround() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.target = null;
        _this.clockwise = true;
        _this.timePerRound = 10;
        _this.faceToTarget = false;
        _this.faceAxis = Axis.NegativeY;
        _this.autoStart = false;
        /** 相对于目标的角度 */
        _this.angle = 0;
        /** 半径 */
        _this.radius = 0;
        /** 标志位，是否正在旋转 */
        _this.isRotating = false;
        return _this;
    }
    RotateAround.prototype.start = function () {
        this.autoStart && this.run();
    };
    RotateAround.prototype.update = function (dt) {
        if (!this.isRotating || !this.target)
            return;
        // 将角度转换为弧度
        var angle = this.angle;
        var radian = (Math.PI / 180) * angle;
        // 更新节点的位置
        var node = this.node, target = this.target, radius = this.radius;
        node.x = target.x + radius * Math.cos(radian);
        node.y = target.y + radius * Math.sin(radian);
        // 更新节点的角度
        if (this.faceToTarget) {
            switch (this.faceAxis) {
                case Axis.PositiveX:
                    node.angle = angle + 180;
                    break;
                case Axis.PositiveY:
                    node.angle = angle + 90;
                    break;
                case Axis.NegativeX:
                    node.angle = angle;
                    break;
                case Axis.NegativeY:
                    node.angle = angle - 90;
                    break;
            }
        }
        // 计算下一帧的角度
        var anglePerFrame = dt * (360 / this.timePerRound);
        angle = this.angle += (this.clockwise ? -anglePerFrame : anglePerFrame);
        // 重置角度，避免数值过大
        if (angle >= 720) {
            this.angle %= 360;
        }
        else if (angle <= -720) {
            this.angle %= -360;
        }
    };
    /**
     * 开始围绕目标节点旋转
     * @param target 目标节点
     * @param clockwise 是否顺时针旋转
     * @param timePerRound 旋转一圈的时间
     * @param faceToTarget 是否始终面向目标节点
     * @param faceAxis 面向目标节点的轴
     */
    RotateAround.prototype.run = function (target, clockwise, timePerRound, faceToTarget, faceAxis) {
        if (target)
            this.target = target;
        if (clockwise)
            this.clockwise = clockwise;
        if (timePerRound)
            this.timePerRound = timePerRound;
        if (faceToTarget)
            this.faceToTarget = faceToTarget;
        if (faceAxis)
            this.faceAxis = faceAxis;
        if (!this.target)
            return cc.log('No target!');
        // 计算初始角度和半径
        this.angle = this.getAngle(this.target.getPosition(), this.node.getPosition());
        this.radius = this.getDistance(this.target.getPosition(), this.node.getPosition());
        // 开始
        this.isRotating = true;
    };
    /**
     * 停止旋转
     */
    RotateAround.prototype.stop = function () {
        this.isRotating = false;
    };
    /**
     * 获取两点间的角度
     * @param p1 点1
     * @param p2 点2
     * @see MathUtil.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/utils/MathUtil.ts
     */
    RotateAround.prototype.getAngle = function (p1, p2) {
        return Math.atan((p2.y - p1.y) / (p2.x - p1.x));
    };
    /**
     * 获取两点间的距离
     * @param p1 点1
     * @param p2 点2
     * @see MathUtil.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/utils/MathUtil.ts
     */
    RotateAround.prototype.getDistance = function (p1, p2) {
        return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
    };
    __decorate([
        property({ type: cc.Node, tooltip: CC_DEV && '围绕旋转的目标' })
    ], RotateAround.prototype, "target", void 0);
    __decorate([
        property({ tooltip: CC_DEV && '顺时针旋转' })
    ], RotateAround.prototype, "clockwise", void 0);
    __decorate([
        property({ tooltip: CC_DEV && '旋转一圈花费的时间' })
    ], RotateAround.prototype, "timePerRound", void 0);
    __decorate([
        property({ tooltip: CC_DEV && '是否始终面向目标节点' })
    ], RotateAround.prototype, "faceToTarget", void 0);
    __decorate([
        property({
            type: cc.Enum(Axis),
            tooltip: CC_DEV && '面向目标节点的轴：\n- PositiveX：正 X 轴\n- PositiveY：正 Y 轴\n- NegativeX：负 X 轴\n- NegativeY：负 Y 轴',
            visible: function () { return this.faceToTarget; }
        })
    ], RotateAround.prototype, "faceAxis", void 0);
    __decorate([
        property({ tooltip: CC_DEV && '自动开始旋转' })
    ], RotateAround.prototype, "autoStart", void 0);
    RotateAround = __decorate([
        ccclass
    ], RotateAround);
    return RotateAround;
}(cc.Component));
exports.default = RotateAround;

cc._RF.pop();